package com.mi.rof.user.service.impl;

import com.mi.rof.user.service.TokenService;
import com.mi.rof.utils.JwtUtils;
import com.mi.rof.entity.vo.UserInfoVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Map;

@Slf4j
@Service
public class TokenServiceImpl implements TokenService {

    @Override
    public UserInfoVo checkToken(String token) {

        if (StringUtils.isBlank(token)) {
            return null;
        }
        Map<String, Object> stringObjectMap = JwtUtils.checkToken(token);
        if (stringObjectMap == null) {
            return null;
        }

        UserInfoVo userInfoVo = new UserInfoVo();
        userInfoVo.setUserId((String) stringObjectMap.get("userId"));
        userInfoVo.setUsername((String) stringObjectMap.get("username"));
        userInfoVo.setEmail((String) stringObjectMap.get("email"));
        userInfoVo.setPhoneNumber((String) stringObjectMap.get("phoneNumber"));
        userInfoVo.setUserRole((Integer) stringObjectMap.get("userRole"));
        return userInfoVo;
    }
}
